package cn.baiweigang.uu.service.linearswap;

import cn.baiweigang.uu.api.linearswap.MarketLinearSwapApi;
import cn.baiweigang.uu.enums.ContractCodeEnum;
import cn.baiweigang.uu.enums.PeriodEnum;
import cn.baiweigang.uu.model.KLineInfo;
import cn.baiweigang.uu.strategy.linearswap.KLineList;
import cn.baiweigang.uu.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KLineService {
    @Autowired
    private MarketLinearSwapApi marketLinearSwapApi;

    public KLineList<KLineInfo> getHistoryKLineMin5(ContractCodeEnum contractCodeEnum,
                                                    int size) {
        List<KLineInfo> kLineInfoList = marketLinearSwapApi.getHistoryData(contractCodeEnum,
                PeriodEnum.MIN5, size);
        KLineList<KLineInfo> kLineList = new KLineList();
        kLineList.setPeriodEnum(PeriodEnum.MIN5);
        for (KLineInfo kLineInfo : kLineInfoList) {
            kLineList.add(kLineInfo);
        }
        return kLineList;
    }

    public void updateToKList(KLineList kLineList, KLineInfo kLineInfo) {
        if (kLineInfo.getId() == kLineList.getLast().getId()) {
            LogUtil.logDebug("还在当前周期内，不需要更新K线图");
            kLineList.set(kLineList.size() - 1, kLineInfo);
            return;
        }
        LogUtil.logInfo("更新K线图 ");
        //去掉最前面的
        kLineList.removeFirst();
        //加到最后
        kLineList.add(kLineInfo);
        //刷新列表
//        kLineList.refreshAll();
    }

}
